#!/usr/bin/env python

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy import sql

egn_papa_admin = create_engine( 'mysql://root:123456@localhost/papa_admin' )

#create_session = sessionmaker(class_=ShardedSession)
#create_ss.configure(shards=egn_papa_datas)

meta=MetaData()

t_user = Table('t_user', meta,
	Column('uid', String(32), nullable=False, primary_key=True, default='' ),
	Column('pwdhash', String(32), nullable=False, default=''),
	#Column('roleid', String(32), nullable=False, default=''),
	#Column('rolevalue', Integer, nullable=False, default=0),
)

t_role = Table('t_role', meta,
	Column('roleid', String(32), nullable=False, primary_key=True, default='' ),
	Column('rolename', String(32), nullable=False, default=''),
	Column('rolevalue', Integer, nullable=False, default=0),
)

t_role_perm = Table('t_role_perm', meta,
	Column('bitid', Integer, nullable=False, primary_key=True, default=0 ),
	Column('bitname', String(32), nullable=False, default=''),
)


class TUser(object):
	def __init__(self, uid, pwdhash):
		self.uid = uid
		self.pwdhash = pwdhash
	def __repr__(self):
		return "<TUser:::uid:%s,pwdhash:%s>" % (self.uid, self.pwdhash)

mapper(TUser, t_user)
SessionPapaAdmin = sessionmaker(bind=egn_papa_admin)
#SessionPapaAdmin.configure(bind=egn_papa_admin) 

if __name__ == '__main__':
	import sys
	if len(sys.argv)>1 and 'recreate'==sys.argv[1] :
		meta.drop_all(egn_papa_admin)
		meta.create_all(egn_papa_admin)
	else:
		ss = SessionPapaAdmin()
		
		#for m in mems:
		#	print m
		for i in range(100):
			ss.save(TUser('usr%02d'%i, '___nothing____'))
		
		ss.commit()
